require "common/Define"
require "framework/BaseController"
require "FairyGUI"

EmitNumbersMain = BaseController:New({mControllerName = "EmitNumbersMain"});

function EmitNumbersMain:Main()
	-- body
	EmitNumbersMain:Awake();
end

function EmitNumbersMain:Awake()
	Application.targetFrameRate = 60;
	Stage.inst.onKeyDown:Add(self.OnKeyDown);

	-- load npc
	local origin = Resources.Load("Role/npc-with-hud");
	local npc1 = GameObject.Instantiate( origin );
	npc1.transform.position = Vector3(0, 0 ,0);
	local npc2 = GameObject.Instantiate( origin );
	npc2.transform.position = Vector3(4, 0 ,0);
	self.mNPC1 = npc1.transform; self.mNPC2 = npc2.transform;

	-- load ui
	local prePackageName = "UI/";
	local packageName = "EmitNumbers";
	UIPackage.AddPackage(prePackageName..packageName);

	local componentName = "Main";
	self.mainComponent = UIPackage.CreateObject(packageName, componentName).asCom;
	GRoot.inst:AddChild( self.mainComponent );

	self:OnViewCreated(self.mainComponent);
end

function EmitNumbersMain:OnKeyDown(context)
 	-- body
 	if (context.inputEvent.keyCode == KeyCode.Escape) then
		print "Exit Game!"
		Application.Quit();
	end
end

function EmitNumbersMain:InitView()
	StartCoroutine(self.RunTest);
end

function EmitNumbersMain:RunTest()
	-- body
	while true do
		math.randomseed(tostring(os.time()):reverse():sub(1, 6));
		EmitManager.inst:Emit(EmitNumbersMain.mNPC1, 0, math.random(100, 100000), math.random(0, 10) == 5);

		math.randomseed(tostring(os.time()):reverse():sub(1, 6));
		EmitManager.inst:Emit(EmitNumbersMain.mNPC2, 1, math.random(100, 100000), math.random(0, 10) == 5);

		WaitForSeconds(0.3);
	end
end